﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using HtmlAgilityPack;
using System.Net.Http;
using System.Net.Http.Headers;

namespace RutTrichThongTin
{
    public class ThongTinTyGia
    {
        Dictionary<string, Double> _DsTyGia;
        private string _TyGiaHienTai;

        public ThongTinTyGia()
        {
            _DsTyGia = new Dictionary<string, double>();
            _DsTyGia.Add("VND", 1);
            TyGiaHienTai = "VND";
        }     

        public string TyGiaHienTai
        {
            get { return _TyGiaHienTai; }
            set { _TyGiaHienTai = value; }
        }

        public Dictionary<string, Double> DsTyGia
        {
            get { return _DsTyGia; }
            set { _DsTyGia = value; }
        }
    }

    public class RutTrichTyGia
    {
        public ThongTinTyGia RutTrichTyGiaNgoaiTe()
        {
            ThongTinTyGia ThongTinTyGia = new ThongTinTyGia();
            var webGet = new HtmlWeb();
            string url = "http://www.vietcombank.com.vn/exchangerates/";
            var document = webGet.Load(url);
            if (document != null)
            {
                HtmlNode NodeTable = document.DocumentNode.SelectSingleNode("//table[@id='ctl00_Content_ExrateView']");
                if (NodeTable != null)
                {
                    HtmlNodeCollection nodeTrs = NodeTable.SelectNodes("child::tr");
                    int n = nodeTrs.Count;
                    for (int i = 1; i < n; i++)
                    {
                        HtmlNodeCollection nodeTds = nodeTrs[i].SelectNodes("child::td");
                        string keyword = nodeTds[0].InnerText;
                        string value = nodeTds[4].InnerText;
                        if (!string.IsNullOrEmpty(keyword) && !string.IsNullOrEmpty(value))
                        {
                            keyword = keyword.Trim();
                            value = value.Trim();
                            ThongTinTyGia.DsTyGia.Add(keyword, Double.Parse(value));
                        }
                    }
                    
                }
            }
            return ThongTinTyGia;
        }
        
    }
}